package com.qijia.travel.helper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 
 * @author chenyh 常用的日期函数
 */
public class DateHelper {

	public static final String yyMMdd = "yy-MM-dd";
	public static final String yyyyMMdd = "yyyy-MM-dd";
	public static final String HHmmss = "HH:mm:ss";
	public static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
	public static final String yyyyMMddHHmm = "yyyy-MM-dd HH:mm";
	public static final String yyMMddHHmmss = "yy-MM-dd HH:mm:ss";

	public static Date parseToDate(String s, String style) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
		simpleDateFormat.applyPattern(style);
		Date date = null;
		if (s == null || s.length() < 8)
			return null;
		try {
			date = simpleDateFormat.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static String parseToString(String s, String style) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
		simpleDateFormat.applyPattern(style);
		Date date = null;
		String str = null;
		if (s == null || s.length() < 8)
			return null;
		try {
			date = simpleDateFormat.parse(s);
			str = simpleDateFormat.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return str;
	}

	public static String parseToString(Date date, String style) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
		simpleDateFormat.applyPattern(style);
		String str = null;
		if (date == null)
			return null;
		str = simpleDateFormat.format(date);
		return str;
	}

	public static String getNowTime() {
		Date nowDate = new Date();
		Calendar now = Calendar.getInstance();
		now.setTime(nowDate);
		SimpleDateFormat formatter = new SimpleDateFormat(yyyyMMddHHmmss);
		String str = formatter.format(now.getTime());
		return str;
	}

	public static String getShortNowTime() {
		Date nowDate = new Date();
		Calendar now = Calendar.getInstance();
		now.setTime(nowDate);
		SimpleDateFormat formatter = new SimpleDateFormat(yyyyMMdd);
		String str = formatter.format(now.getTime());
		return str;
	}
	
	public static String getThisMonthFirstDay(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar
				.getActualMinimum(Calendar.DAY_OF_MONTH));
		SimpleDateFormat formatter = new SimpleDateFormat(yyyyMMdd);
		String str = formatter.format((calendar.getTime()));
		return str;
	}
	
	public static String getThisMonthFirstDay(Calendar calendar){
		calendar.set(Calendar.DAY_OF_MONTH, calendar
				.getActualMinimum(Calendar.DAY_OF_MONTH));
		SimpleDateFormat formatter = new SimpleDateFormat(yyyyMMdd);
		String str = formatter.format((calendar.getTime()));
		return str;
	}
	
	public static String getThisMonthEndDay(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar
				.getActualMaximum(Calendar.DAY_OF_MONTH));
		SimpleDateFormat formatter = new SimpleDateFormat(yyyyMMdd);
		String str = formatter.format((calendar.getTime()));
		return str;
	}
	
	public static String getThisMonthEndDay(Calendar calendar){
		calendar.set(Calendar.DAY_OF_MONTH, calendar
				.getActualMaximum(Calendar.DAY_OF_MONTH));
		SimpleDateFormat formatter = new SimpleDateFormat(yyyyMMdd);
		String str = formatter.format((calendar.getTime()));
		return str;
	}

	public static String getMonthFirstDay() {
		Calendar calendar = Calendar.getInstance();
		return getThisMonthFirstDay(calendar);
	}
	
	public static String getMonthEndDay() {
		Calendar calendar = Calendar.getInstance();
		return getThisMonthEndDay(calendar);
	}

	public static String addDay(String s, int n) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMdd);
			return addDay(sdf.parse(s), n);
		} catch (Exception e) {
			return null;
		}
	}

	public static String addDay(Date s, int n) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMdd);
			Calendar cd = Calendar.getInstance();
			cd.setTime(s);
			cd.add(Calendar.DATE, n);
			return sdf.format(cd.getTime());
		} catch (Exception e) {
			return null;
		}
	}

	public static boolean DateCompare(Date d1, Date d2) {
		Calendar c1 = Calendar.getInstance();
		c1.setTime(d1);
		Calendar c2 = Calendar.getInstance();
		c2.setTime(d2);
		if (c1.compareTo(c2) == 1) {
			return true;
		}
		return false;
	}

	public static boolean DateCompare(String d1, String d2, String dateModel)
			throws ParseException {
		SimpleDateFormat d = new SimpleDateFormat(dateModel);
		Calendar c1 = Calendar.getInstance();
		c1.setTime(d.parse(d1));
		Calendar c2 = Calendar.getInstance();
		c2.setTime(d.parse(d2));
		if (c1.compareTo(c2) == 1) {
			return true;
		}
		return false;
	}

	public static Date getNowUtilLongDate() {
		return DateHelper.parseToDate(DateHelper.getNowTime(),
				DateHelper.yyyyMMddHHmmss);
	}

	public static Date getNowUtilShortDate() {
		return DateHelper
				.parseToDate(DateHelper.getNowTime(), DateHelper.yyyyMMdd);
	}

	public static String getDescNowTime() {
		Calendar calendar = Calendar.getInstance();
		// calendar.set(2010,0,24);
		String date = "";
		date += calendar.get(Calendar.YEAR) + "年"
				+ (calendar.get(Calendar.MONTH) + 1) + "月"
				+ calendar.get(Calendar.DATE) + "日";

		int day = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (day == 0) {
			date += " 星期日 ";
		} else if (day == 1) {
			date += " 星期一";
		} else if (day == 2) {
			date += " 星期二 ";
		} else if (day == 3) {
			date += " 星期三 ";
		} else if (day == 4) {
			date += " 星期四 ";
		} else if (day == 5) {
			date += " 星期五 ";
		} else if (day == 6) {
			date += " 星期六 ";
		}
		if (calendar.get(Calendar.AM_PM) == 0) {
			date += "AM";
		} else {
			date += "PM";
		}
		return date;
	}

	public static int getAge(Date birthday) {
		Calendar rightNow = Calendar.getInstance();
		Calendar ageCale = Calendar.getInstance();

		ageCale.setTime(birthday);
		return rightNow.get(Calendar.YEAR) - ageCale.get(Calendar.YEAR);
	}

	/**
	 * 
	 * 方法描述：
	 * 计算日期相差天数
	 * 创建时间：2011-2-26 下午01:33:09 
	 * @param d1
	 * @param d2
	 * @return
	 */
    public static int getDaysBetween(Date d1, Date d2) {
    	Calendar c1 = Calendar.getInstance();
    	c1.setTime(d1);
    	Calendar c2 = Calendar.getInstance();
    	c2.setTime(d2);
    	
		int days = c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR);
		int y2 = c2.get(Calendar.YEAR);
		if (c1.get(Calendar.YEAR) != y2) {
			days += c1.getActualMaximum(Calendar.DAY_OF_YEAR);
			c1.add(Calendar.YEAR, 1);
		}
		return days;
	}
    
    public  static void main(String[] args){
    	System.out.println(getMonthFirstDay()+"~~"+getMonthEndDay());
    }


}